using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Web.BL.Category;

public partial class admin_webparts_Plug_ins_Category_Default : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString["action"];
        string controlName = null;
        if (action != null)
        {
            if (action.Equals("addcat", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "CategoryInput.ascx";
            }
            else if (action.Equals("editcat", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "CategoryInput.ascx";
            }
            else if (action.Equals("deletecat", StringComparison.OrdinalIgnoreCase))
            {
                this.DeleteCategory(Convert.ToInt32(Request.QueryString["id"]));
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"], false);
                return;
            }
            else if (action.Equals("viewcat", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "CategoryDetail.ascx";
            }
            else if (action.Equals("additem", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "ItemInput.ascx";
            }
            else if (action.Equals("edititem", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "ItemInput.ascx";
            }
            else if (action.Equals("deleteitem", StringComparison.OrdinalIgnoreCase))
            {
                this.DeleteItem(Convert.ToInt32(Request.QueryString["id"]));
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewcat&id=" + Request.QueryString["catid"], false);
                return;
            }
            else if (action.Equals("moveitem", StringComparison.OrdinalIgnoreCase))
            {
                if ("up".Equals(Request.QueryString["move"], StringComparison.OrdinalIgnoreCase))
                {
                    this.MoveItemUp(Convert.ToInt32(Request.QueryString["id"]));
                }
                else if ("down".Equals(Request.QueryString["move"], StringComparison.OrdinalIgnoreCase))
                {
                    this.MoveItemDown(Convert.ToInt32(Request.QueryString["id"]));
                }
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewcat&id=" + Request.QueryString["catid"], false);
                return;
            }
            else if (action.Equals("defaultselect", StringComparison.OrdinalIgnoreCase))
            {
                this.SetDefaultSelect(Convert.ToInt32(Request.QueryString["id"]), true);
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewcat&id=" + Request.QueryString["catid"], false);
                return;
            }
            else if (action.Equals("defaultnotselect", StringComparison.OrdinalIgnoreCase))
            {
                this.SetDefaultSelect(Convert.ToInt32(Request.QueryString["id"]), false);
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewcat&id=" + Request.QueryString["catid"], false);
                return;
            }
            else
            {
                controlName = "CategoryList.ascx";
            }
        }
        else
        {
            controlName = "CategoryList.ascx";
        }

        if (controlName != null)
        {
            Control ctrl = LoadControl(controlName);
            this.Controls.Add(ctrl);
        }
    }

    private void MoveItemUp(int id)
    {
        try
        {
            CatItem item = new CatItem(id);
            item.MoveUp();
        }
        catch (Exception) { }
    }

    private void MoveItemDown(int id)
    {
        try
        {
            CatItem item = new CatItem(id);
            item.MoveDown();
        }
        catch (Exception) { }
    }

    private void DeleteItem(int id)
    {
        try
        {
            CatItem item = new CatItem(id);
            item.Delete();
        }
        catch (Exception) { }
    }

    private void DeleteCategory(int id)
    {
        try
        {
            Category cat = new Category(id);

            DataTable tb = cat.CatItems;
            if (tb != null)
            {
                foreach (DataRow row in tb.Rows)
                {
                    int itemID = (int)row[CatItem.ID__COLUMN_NAME];
                    (new CatItem(itemID)).Delete();
                }
            }

            cat.Delete();
        }
        catch (Exception) { }
    }

    private void SetDefaultSelect(int id, bool isDefault)
    {
        try
        {
            CatItem item = new CatItem(id);
            Category cat = new Category(item.CatID);

            if (isDefault && cat.SelectType == SelectType.SingleSelect)
            {
                cat.ClearDefaultSelect();
            }

            item.DefaultSelect = isDefault;
            item.Update();
        }
        catch (Exception) { }
    }
}
